package com.baosight.sczx.zm.ck.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.zm.ck.domain.ZMCKJxAp;

/**
 * 
 * <p>Title: ServiceZMCK50.java</p>
 * <p>Description: 下井检查记录管理</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2016年9月13日 下午3:18:36  郭淑芳  Create
 *</pre>
 */
public class ServiceZMCK50 extends ServiceEPBase {
	/**
	 * 
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始进入方法</p>
	 * <p>return value: ServiceZMCK50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月13日下午3:18:45 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: 查询方法</p>
	 * <p>return value: ServiceZMCK50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月13日下午3:18:52 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCKJxAp.query", new ZMCKJxAp());
		
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.ch.status", new String[] { "草拟", "上报", "已发布" }, new String[] { "草拟", "上报", "已发布" }, false));// 状态
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false, UserSession.getInSessionProperty("miningCode").toString()));
		
		outInfo.set("fdCreatorId", UserSession.getUserId());// 登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));// 单位的默认值
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));// 单位的默认值
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));// 矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));// 矿名称的默认值
		return outInfo;
	}

	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: 新增方法</p>
	 * <p>return value: ServiceZMCK50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月13日下午3:19:03 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");// 矿ID的默认值
		EiInfo out = new EiInfo();
		ZMCKJxAp _dom = new ZMCKJxAp();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdReportPerson(UserSession.getInSessionProperty("UserCname").toString());
			_dom.setFdMiningId(fdMiningId);

			Map<String, String> params = new HashMap<String, String>();
			boolean validate = validate(inInfo, _dom, params);
			if (validate) {
				out = this.query(inInfo);
				return out;
			} else {
				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CK_JXAP"));
				this.dao.insert("ZMCKJxAp.insert", _dom);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * 
	 * <p>Function: update</p>
	 * <p>Description: 修改方法</p>
	 * <p>return value: ServiceZMCK50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月13日下午3:16:34 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCKJxAp _dom = new ZMCKJxAp();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

			boolean validate = validate(inInfo, _dom, params);
			if (validate) {
				out = this.query(inInfo);
				return out;
			} else {
				_dom.setFdCurrentDate(StaticTools.getDate());
				this.dao.update("ZMCKJxAp.update", _dom);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * 
	 * <p>Function: delete</p>
	 * <p>Description: 删除方法</p>
	 * <p>return value: ServiceZMCK50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月13日下午3:16:45 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {

		EiInfo _temp = super.delete(inInfo, "ZMCKJxAp.delete");

		EiInfo out = new EiInfo();
		out = this.query(inInfo);
		out.setStatus(_temp.getStatus());
		out.setMsg(_temp.getMsg());

		return out;
	}

	/**
	 * 
	 * <p>Function: report</p>
	 * <p>Description: 上报方法</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月13日下午3:17:01 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo, "ZMCKJxAp.report");

		EiInfo out = new EiInfo();
		out = this.query(inInfo);
		out.setStatus(_temp.getStatus());
		out.setMsg(_temp.getMsg());

		return out;
	}

	/**
	 * 
	 * <p>Function: validate</p>
	 * <p>Description:校验是否重复 </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月13日下午3:17:20 郭淑芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	private boolean validate(EiInfo inInfo, ZMCKJxAp _dom, Map<String, String> params) {
		if (params == null) {
			params = new HashMap<String, String>();
		}
		String fdMiningId = _dom.getFdMiningId();
		String fdJxBm = _dom.getFdJxBm();
		String fdJxTime = _dom.getFdJxTime();
		params.put("fdMiningId", fdMiningId);
		params.put("fdJxBm", fdJxBm);
		params.put("fdJxTime", fdJxTime);
		List result = dao.query("ZMCKJxAp.validate", params);
		if (result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "检修安排每矿每部门每天只能填写一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}
}
